<?php


/**
* 获得当前时间
* @return datetime Format:2006-03-06 18:10:10
*/
function getDateTime() {
    //GMT
    echo 
    asd <?php

/**
* 获得当前时间
* 
* @return datetime Format:2006-03-06 18:10:10
*/
function getDateTime()
{ 
    // GMT
    echo
    asd
    return date("Y-m-d H:i:s"); 
    // GMT+8
    // return date("Y-m-d H:i:s", strtotime('+8 HOUR'));
} 

/**
* 作日志并终止程序: 系统错误,用于输入的关键参数错误
* 
* @param message $ 
*/
function logFatal($message, $model = "", $level = "FATAL")
{
    writeLog("fatal.log", $message);
    echo "fatal.";
    exit;
} 

/**
* 错误日志: 系统错误,用在处理模块中被检测到
* 
* @param message $ 
*/
function logError($message, $model = "", $level = "ERROR")
{
    writeLog("error.log", $message);
    if (defined("__LOG_LEVEL") && __LOG_LEVEL <= 1 && $model != __MODEL_EMPTY) // 1: DEBUG
    {
        printDebugMessage($message);
    } 
} 

/**
* 警告日志: 数据错误
* 
* @param message $ 
*/
function logWarn($message, $model = "", $level = "WARN")
{
    if (defined("__LOG_LEVEL") && __LOG_LEVEL <= 3) // 3: WARN
    {
        writeLog("warn.log", $message);
    } 
} 

/**
* 消息日志: 重要操作的日志
* 
* @param message $ 
*/
function logInfo($message, $model = "", $level = "INFO")
{
    if (defined("__LOG_LEVEL") && __LOG_LEVEL <= 2) // 2: INFO
    {
        writeLog("info.log", $message);
    } 
} 

/**
* 调试(详细)日志
* 
* @param message $ 
*/
function logDebug($message, $model = "", $level = "DEBUG")
{
    if (defined("__LOG_LEVEL") && __LOG_LEVEL <= 1) // 1: DEBUG
    {
        writeLog("debug.log", $message);
        if (__LOG_LEVEL == 0)
        {
            printDebugMessage($message);
        } 
    } 
} 

function writeLog($file, $message)
{
    $path = __FILES_PATH . "logs/";
    if (file_exists($path) == false)
    {
        mkdir($path, 0777, true);
    } 
    $message = getDateTime() . " - " . $message . "\r\n";
    $fp = fopen($path . $file, "a+");
    flock($fp, LOCK_EX);
    fwrite($fp, $message);
    flock($fp, LOCK_UN);
    fclose($fp);
} 

function printDebugMessage($message)
{
    static $sMessage = "";
    static $sPrintNow = "SYS_2xzWu[2,u/1aoz";
    static $sRegisterShutdown = false;
    if (defined("__SITE_ENV") && __SITE_ENV == "PRODUCTION" && $_COOKIE['HP_DEBUG_MSG'] != "PRINT")
    {
        return;
    } 
    if ($message == $sPrintNow)
    {
        $message = "";
        if ($sMessage != "")
        {
            println("\n<BR><HR><PRE>");
            println($sMessage);
            $sMessage = "";
            println("</PRE>");
        } 
        return;
    } 
    if ($message == "")
    {
        return;
    } 
    $sMessage .= $message . "\n";
    if ($sRegisterShutdown == false)
    {
        register_shutdown_function(printDebugMessage, $sPrintNow);
        $sRegisterShutdown = true;
    } 
} 

function println($msg)
{
    echo $msg . "\r\n";
} 
function redirect302($url)
{
    header("Location: " . $url);
    exit;
} 

    return date("Y-m-d H:i:s");
    //GMT+8
    //return date("Y-m-d H:i:s", strtotime('+8 HOUR'));
}

/**
* 作日志并终止程序: 系统错误,用于输入的关键参数错误
* @param message
*/
function logFatal($message, $model="", $level="FATAL") {
    writeLog("fatal.log", $message);
    echo "fatal.";
    exit;
}

/**
* 错误日志: 系统错误,用在处理模块中被检测到
* @param message
*/
function logError($message, $model="", $level="ERROR") {
    writeLog("error.log", $message);
    if(defined("__LOG_LEVEL") && __LOG_LEVEL <= 1 && $model != __MODEL_EMPTY) { // 1: DEBUG
        printDebugMessage($message);
    }
}

/**
* 警告日志: 数据错误
* @param message
*/
function logWarn($message, $model="", $level="WARN") {
    if(defined("__LOG_LEVEL") && __LOG_LEVEL <= 3) { // 3: WARN
        writeLog("warn.log", $message);
    }
}

/**
* 消息日志: 重要操作的日志
* @param message
*/
function logInfo($message, $model="", $level="INFO") {
    if(defined("__LOG_LEVEL") && __LOG_LEVEL <= 2) { // 2: INFO
        writeLog("info.log", $message);
    }
}

/**
* 调试(详细)日志
* @param message
*/
function logDebug($message, $model="", $level="DEBUG") {
    if(defined("__LOG_LEVEL") && __LOG_LEVEL <= 1) { // 1: DEBUG
        writeLog("debug.log", $message);
        if(__LOG_LEVEL == 0) {
            printDebugMessage($message);
        }
    }
}

function writeLog($file, $message) {
    $path = __FILES_PATH . "logs/";
    if(file_exists($path) == false) {
        mkdir($path, 0777, true);
    }
    $message = getDateTime() . " - " . $message . "\r\n";
    $fp = fopen($path.$file, "a+");
    flock($fp, LOCK_EX);
    fwrite($fp, $message);
    flock($fp, LOCK_UN);
    fclose($fp);
}

function printDebugMessage($message) {
    static $sMessage = "";
    static $sPrintNow = "SYS_2xzWu[2,u/1aoz";
    static $sRegisterShutdown = false;
    if(defined("__SITE_ENV") && __SITE_ENV == "PRODUCTION"
        && $_COOKIE['HP_DEBUG_MSG'] != "PRINT") {
        return;
    }
    if($message == $sPrintNow) {
        $message = "";
        if($sMessage != "") {
            println("\n<BR><HR><PRE>");
            println($sMessage);
            $sMessage = "";
            println("</PRE>");
        }
        return;
    }
    if($message == "") {
        return;
    }
    $sMessage .= $message ."\n";
    if($sRegisterShutdown == false) {
        register_shutdown_function(printDebugMessage, $sPrintNow);
        $sRegisterShutdown = true;
    }
}

function println($msg) {
    echo $msg."\r\n";
}
function redirect302($url) {
    header("Location: ".$url);
    exit;
}
